package com.example.foodisimportant.StudentViewActivity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.foodisimportant.Body.ResponseBody;
import com.example.foodisimportant.Body.dishBody;
import com.example.foodisimportant.Canteens.Canteens_Activity;
import com.example.foodisimportant.Canteens.SearchInformationActivity;
import com.example.foodisimportant.Network.NetworkSettings;
import com.example.foodisimportant.Network.SearchNetworkThread;
import com.example.foodisimportant.R;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.FutureTask;

public class HomeFragment extends Fragment{
    private Button mBtn1,mBtn3,mBtn4,mBtn5;
    private EditText mEtsearch;
    private String searchchar;
    private final ObjectMapper mapper = new ObjectMapper();
    private View homeLayout;
    final Bundle bundle = new Bundle();
    private int previousSelectedPosition = 0;
    boolean isRunning = false;
    //轮播图
    private ViewPager mViewPaper;
    private List<ImageView> images;
    private List<View> dots;
    private int currentItem;
    private TextView title;
    private ChangeHomePictureAdapter changeHomePictureAdapter;
    //记录上一次点的位置
    private int oldPosition=0;
    //存放图片的id
    private int[] imageIds = new int[]{
            R.mipmap.a,
            R.mipmap.b,
            R.mipmap.c,
            R.mipmap.d,
    };
    //存放图片的标题
    private String[] titles = new String[]{
            "干净又卫生",
            "好吃又便宜",
            "你想吃的都在这里",
            "没有人不爱天大食堂",
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        homeLayout=inflater.inflate(R.layout.fragment_home,container,false);
        setRecommend();
        mEtsearch=homeLayout.findViewById(R.id.first_page_search);
        mEtsearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    searchchar=mEtsearch.getText().toString();
                    //拉取
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent=new Intent(getActivity(), SearchInformationActivity.class);
                            intent.putExtra("keyword",searchchar);
                            startActivity(intent);
                        }
                    }).start();
                    Toast.makeText(getActivity(), searchchar, Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

        // 开启轮询
        new Thread(new Runnable() {
            @Override
            public void run() {
                isRunning=true;
                while (isRunning){
                    try {
                        Thread.sleep(2000);//暂停两秒
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int loop=0;
                            if(mViewPaper.getCurrentItem()<3){
                                mViewPaper.setCurrentItem(mViewPaper.getCurrentItem()+1);
                            }else {
                                mViewPaper.setCurrentItem(loop);
                            }

                        }
                    });
                }
            }
        }).start();
        return homeLayout;
    }

    private void setRecommend(){
        mViewPaper=homeLayout.findViewById(R.id.recommend_vp);
        mBtn1=homeLayout.findViewById(R.id.number1);
        mBtn3=homeLayout.findViewById(R.id.number3);
        mBtn4=homeLayout.findViewById(R.id.number4);
        mBtn5=homeLayout.findViewById(R.id.number5);

        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putInt("status",1);
                Intent intent=new Intent(getActivity(), Canteens_Activity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mBtn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putInt("status",3);
                Intent intent=new Intent(getActivity(), Canteens_Activity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mBtn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putInt("status",4);
                Intent intent=new Intent(getActivity(), Canteens_Activity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mBtn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putInt("status",5);
                Intent intent=new Intent(getActivity(), Canteens_Activity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //显示图片
        images=new ArrayList<ImageView>();
        for (int i=0;i<imageIds.length;i++){
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imageIds[i]);
            images.add(imageView);
        }

        //显示小点
        dots = new ArrayList<View>();
        dots.add(homeLayout.findViewById(R.id.dot_1));
        dots.add(homeLayout.findViewById(R.id.dot_2));
        dots.add(homeLayout.findViewById(R.id.dot_3));
        dots.add(homeLayout.findViewById(R.id.dot_4));
        title = homeLayout.findViewById(R.id.recommed_title);
        title.setText(titles[0]);
        changeHomePictureAdapter=new ChangeHomePictureAdapter();
        mViewPaper.setAdapter(changeHomePictureAdapter);
        mViewPaper.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
            @Override
            public void onPageSelected(int position) {
                title.setText(titles[position]);
                dots.get(position).setBackgroundResource(R.mipmap.dot_yes);
                dots.get(oldPosition).setBackgroundResource(R.mipmap.dot_no);
                oldPosition=position;
                currentItem=position;
            }
            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }



    public class ChangeHomePictureAdapter extends PagerAdapter{

        @Override
        public int getCount() { return images.size(); }

        @Override
        public boolean isViewFromObject(View view, Object object) { return view==object; }

        @Override
        public void destroyItem( ViewGroup container, int position,Object object) {
            container.removeView(images.get(position));
        }
        @Override
        public Object instantiateItem( ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }
    }

}
